using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProjectileMelee:Projectile {

	protected override void Init() {
		base.Init();

		timeHit= 0;
		disabled=false;

		Vector2Int start = Vector2Int.FloorToInt(collider.bounds.min);
		Vector2Int end = Vector2Int.CeilToInt(collider.bounds.max);

		for(int x = start.x;x<=end.x;x++) {
			for(int y = start.y;y<=end.y;y++) {
				Vector2Int pos = new Vector2Int(x,y);
				if(!TilemapController.SolidAt(pos)) continue;
				TilemapController.DamageTile(pos,getDamageStat);
			}
		}
	}

	protected override void Start() {
		base.Start();
		spriteRenderer=GetComponent<SpriteRenderer>();
	}

	protected override void Update() {
		base.Update();

		spriteRenderer.sprite=sprites[
			Mathf.Min(Mathf.FloorToInt(sprites.Length*(time/stat.timeTotal)),sprites.Length-1)
		];

		if(((Angle)transform.rotation).IfBetween(Angle.up,Angle.down)) spriteRenderer.flipY=true;
		else spriteRenderer.flipY=false;
	}

	protected override void UpdatePosition() {
		transform.position+=(Vector3)forward*Time.deltaTime;
	}

	[SerializeField] Sprite[] sprites;
	protected SpriteRenderer spriteRenderer;

	protected int timeHit;
	protected bool disabled;

	protected override bool HitMob(Mob other,Vector2 normal,Vector2 position) {
		if(!isActiveAndEnabled) return false;
		if(disabled) return false;

		timePenetrated=-1;
		bool result = base.HitMob(other,normal,position);

		if(timeHit>=Mathf.FloorToInt(stat.penetration)) {
			bool endProjectile = true;
			if(timeHit>=Mathf.CeilToInt(stat.penetration)) endProjectile=true;
			else if(Utility.Chance(stat.penetration-Mathf.Floor(stat.penetration))) endProjectile=false;
			if(endProjectile) disabled=true;
		}
		timeHit++;
		return result;

	}

}
